package me.zhengjie.modules.web.service.pay.order;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import me.zhengjie.config.exception.WeChatException;
import me.zhengjie.constant.ConstantUtils;
import me.zhengjie.modules.system.domain.IdentityOrder;
import me.zhengjie.modules.system.service.IdentityItemService;
import me.zhengjie.modules.system.service.IdentityOrderService;
import me.zhengjie.modules.system.service.dto.*;
import me.zhengjie.modules.system.service.mapstruct.WxUserExpMapper;
import me.zhengjie.modules.web.domain.dis.DisOrderCalcDTO;
import me.zhengjie.modules.web.domain.pay.OrderSubmitRequest;
import me.zhengjie.modules.web.domain.pay.OrderSubmitResponse;
import me.zhengjie.modules.web.enums.OrderStatusEnum;
import me.zhengjie.modules.web.service.dis.IDisOrderWebService;
import me.zhengjie.modules.web.service.order.IOrderService;
import me.zhengjie.modules.web.service.pay.IOrderSubmitService;
import me.zhengjie.modules.weixin.utils.OrderUtil;
import me.zhengjie.request.ATMRContext;
import me.zhengjie.utils.DateUtil;
import me.zhengjie.utils.StringUtils;
import me.zhengjie.utils.enums.OrderPrefixEnum;
import me.zhengjie.utils.enums.OrderTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

import static me.zhengjie.constant.ConstantUtils.DisOrderType_Identity;

@Service
public class IdentityOrderSubmitServiceImpl implements IOrderSubmitService {
    @Autowired
    private IdentityOrderService identityOrderService;

    @Autowired
    private IdentityItemService identityItemService;

    @Autowired
    private WxUserExpMapper wxUserExpMapper;

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IDisOrderWebService disOrderWebService;


    @Override
    public int orderType() {
        return OrderTypeEnum.Identity.getOrderType();
    }

    @Override
    public OrderSubmitResponse execute(OrderSubmitRequest request) {
        if (StringUtils.isBlank(request.getIdentityNo())){
            throw new WeChatException("缺少参数identityNo");
        }
        IdentityItemQueryCriteria criteria = new IdentityItemQueryCriteria();
        criteria.setItemNo(request.getIdentityNo());
        criteria.setStatus(ConstantUtils.Valid);
        List<IdentityItemDto> itemDtoList = identityItemService.queryAll(criteria);
        if (CollectionUtil.isEmpty(itemDtoList)){
            throw new WeChatException("参数传递有误");
        }
        String openId = ATMRContext.getContext().getOpenId();
        IdentityItemDto identityItemDto = itemDtoList.get(0);
        BigDecimal salePrice = identityItemDto.getSalePrice();
        String orderSn = OrderUtil.GetOrderNumber(OrderPrefixEnum.Identity.getPrefix());
        IdentityOrder identityOrder = new IdentityOrder();
        identityOrder.setIdentityAmount(salePrice);
        identityOrder.setOpenId(openId);
        identityOrder.setOrderNo(orderSn);
        identityOrder.setCreateTime(DateUtil.TimestampNow());
        identityOrder.setOrderType(request.getIdentityType());
        identityOrder.setPayAmount(BigDecimal.ZERO);
        identityOrder.setDiscountAmount(BigDecimal.ZERO);
        identityOrder.setPaySerialid("");
        identityOrder.setPrePayid("");
        identityOrder.setPayStatus(OrderStatusEnum.WaitingPay.getOrderStatus());
        identityOrder.setOrderStatus(OrderStatusEnum.WaitingPay.getOrderStatus());
        identityOrder.setBizId(request.getIdentityNo());
        identityOrder.setRemark(identityItemDto.getTitle());
        Timestamp timestamp = Timestamp.valueOf(LocalDateTime.now().plusMinutes(ConstantUtils.DefaultExpireTime));
        identityOrder.setExpireTime(timestamp);
        if (salePrice.compareTo(BigDecimal.ZERO) == 0){
            identityOrder.setPayStatus(OrderStatusEnum.Payed.getOrderStatus());
            identityOrder.setOrderStatus(OrderStatusEnum.Payed.getOrderStatus());
            WxUserDto wxUserDto = new WxUserDto();
            wxUserDto.setIdentityNo(identityItemDto.getItemNo());
            LambdaQueryWrapper<WxUserDto> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(WxUserDto::getOpenId,openId);
            wxUserExpMapper.update(wxUserDto,wrapper);

            //计算佣金
            DisOrderCalcDTO disOrderCalcDTO = new DisOrderCalcDTO();
            disOrderCalcDTO.setOrderSn(orderSn);
            disOrderCalcDTO.setOrderAmount(RandomUtil.randomBigDecimal(BigDecimal.TEN,BigDecimal.valueOf(1000)));
            disOrderCalcDTO.setOpenId(openId);
            disOrderCalcDTO.setOrderType(DisOrderType_Identity);
            disOrderCalcDTO.setTitle(identityItemDto.getTitle());
            disOrderWebService.insertDisOrder(disOrderCalcDTO);
        }
        IdentityOrderDto examOrderDto = identityOrderService.create(identityOrder);
        OrderSubmitResponse response = new OrderSubmitResponse();
        response.setOrderId(examOrderDto.getOrderId());
        response.setOrderSn(orderSn);
        response.setOrderStatus(identityOrder.getOrderStatus());

        orderService.orderExpire2Redis(orderType(),orderSn,timestamp);
        return response;
    }
}
